﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack.Redis;
using ServiceStack.Text;
using MySql.Data.MySqlClient;
using Spider.Framework.DataAccess;
using System.IO;

namespace DataPersonalization.Utis
{
    public class RedisAccess
    {
        public void GetLinks()
        {
            try
            {
                string pattFolder = @"Files/";

                if (Directory.Exists(pattFolder))
                {
                    string[] arrFile = Directory.GetFiles(pattFolder);
                    MyUtis utis = new MyUtis();

                    if (arrFile.Length > 0)
                    {
                        ParserObject.LoadConfig();
                        int total = (arrFile.Length) * 10000;
                        int tSuccess = 0;
                        int tFalse = 0;

                        int no = 1;
                        for (int i = 0; i < arrFile.Length; i++)
                        {
                            try
                            {
                                string fileName = arrFile[i];
                                string logFileName = fileName.Replace("Files/","");

                                List<Links> ls = utis.GetListLinksFromFile(fileName);

                                if (ls != null && ls.Count > 0)
                                {
                                    for (int k = 0; k < ls.Count; k++)
                                    {
                                        string id = ls[k].Id;
                                        string url = ls[k].Link;

                                        try
                                        {
                                            utis.TraceLog_Total(total, tSuccess, tFalse);

                                            var obj = ParserObject.Parser(url);
                                            if (obj.Type == ParserObject.ResultType.Success && obj.Entity != null)
                                            {
                                                Article art = new Article();

                                                art.Category_name = "";
                                                art.Content = obj.Entity.Content;
                                                art.Createdate = DateTime.Now;
                                                art.Description = obj.Entity.Summury;
                                                art.Id = id;
                                                art.Title = obj.Entity.Title;
                                                art.Url = url;

                                                AddToDb(art);

                                                tSuccess = tSuccess + 1;
                                                utis.TraceLog_Total(total, tSuccess, tFalse);
                                            }
                                            else
                                            {
                                                // ghi xuống file logs
                                                Links lnk = new Links();

                                                lnk.No = no;
                                                lnk.Id = id;
                                                lnk.Link = url;
                                                lnk.Status = obj.Type.ToString();

                                                utis.TraceLog_Status(lnk, logFileName);

                                                no = no + 1;

                                                tFalse = tFalse + 1;
                                                utis.TraceLog_Total(total, tSuccess, tFalse);
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            // ghi xuống file logs
                                            Links lnk = new Links();

                                            lnk.No = no;
                                            lnk.Id = id;
                                            lnk.Link = url;
                                            lnk.Status = ex.Message;

                                            utis.TraceLog_Status(lnk, logFileName);

                                            no = no + 1;

                                            tFalse = tFalse + 1;
                                            utis.TraceLog_Total(total, tSuccess, tFalse);
                                        }
                                    }
                                }
                            }
                            catch (Exception ex) 
                            { 
                                string s = ex.Message; 
                            }
                        }
                    }
                }

                /*
                using (var redisManager = new PooledRedisClientManager(new[] { Config.host + ":" + Config.port }))
                using (var redis = redisManager.GetClient())
                {
                    var dict = redis.GetAllEntriesFromHash("personalization");
                    // lưu xuống file txt
                    if (dict != null)
                    {
                        int t = 1; // mỗi file 10.000 record
                        int file = 1;
                        string filename = "1.txt";

                        foreach (var entry in dict)
                        {
                            if (t > 10000)
                            {
                                file=file + 1;
                                filename = file.ToString() + ".txt";

                                t = 1; // gán về 1
                            }

                            WriteLink(filename, t, entry.Key, entry.Value);
                            t = t + 1;
                        }
                    }
                    

                    if (dict != null)
                    {
                        int total = dict.Keys.Count;
                        int tSuccess = 0;
                        int tFalse = 0;

                        TraceLog_Total(total, tSuccess, tFalse);

                        ParserObject.LoadConfig();
                        int i = 1;
                        foreach (var entry in dict)
                        {
                            try
                            {
                                string url = entry.Value;
                                var obj = ParserObject.Parser(url);

                                if (obj.Type == ParserObject.ResultType.Success && obj.Entity != null)
                                {
                                    Article art = new Article();

                                    art.Category_name = "";
                                    art.Content = obj.Entity.Content;
                                    art.Createdate = DateTime.Now;
                                    art.Description = obj.Entity.Summury;
                                    art.Id = entry.Key;
                                    art.Title = obj.Entity.Title;
                                    art.Url = url;

                                    AddToDb(art);

                                    tSuccess = tSuccess + 1;
                                    TraceLog_Total(total, tSuccess, tFalse);
                                }
                                else
                                {
                                    // ghi xuống file logs
                                    Links lnk = new Links();

                                    lnk.No = i;
                                    lnk.Id = entry.Key;
                                    lnk.Link = entry.Value;
                                    lnk.Status = obj.Type.ToString();

                                    TraceLog_Status(lnk);

                                    i = i + 1;

                                    tFalse = tFalse + 1;
                                    TraceLog_Total(total, tSuccess, tFalse);
                                }
                            }
                            catch (Exception ex)
                            {
                                string s = ex.Message;
                            }
                        }
                    }

                   
                } */
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }

        public void AddToDb(Article art)
        {
            try
            {
                string con = Config.conn;

                const string sql = "INSERT INTO contents_personalization(id, title, description, content, url, category_name, createdate) " +
                                  "VALUES(@id, @title, @description, @content, @url, @category_name, @createdate)";

                var param = new[]
                            {
                                new MySqlParameter("@id", art.Id),
                                new MySqlParameter("@title", art.Title),
                                new MySqlParameter("@description", art.Description),
                                new MySqlParameter("@content", art.Content),
                                new MySqlParameter("@url", art.Url),
                                new MySqlParameter("@category_name", art.Category_name),
                                new MySqlParameter("@createdate", art.Createdate)
                            };

                (new MySqlBaseDAO(con)).ExeNonQuery(sql, param);
            }
            catch
            { }
        }

        
    }

    public class Links
    {
        private int _no;

        public int No
        {
            get { return _no; }
            set { _no = value; }
        }
        private string _id;

        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }
        private string _link;

        public string Link
        {
            get { return _link; }
            set { _link = value; }
        }
        private string _status;

        public string Status
        {
            get { return _status; }
            set { _status = value; }
        }
    }

    public class Article
    {
        private string _id;
        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _title;
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        private string _description;
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        private string _content;
        public string Content
        {
            get { return _content; }
            set { _content = value; }
        }

        private string _url;
        public string Url
        {
            get { return _url; }
            set { _url = value; }
        }

        private string _category_name;
        public string Category_name
        {
            get { return _category_name; }
            set { _category_name = value; }
        }

        private DateTime _createdate;
        public DateTime Createdate
        {
            get { return _createdate; }
            set { _createdate = value; }
        }
    }
}
